﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ProductRatingRepository : Data.Repository.IProductRatingRepository
    {
        private LinqToECommerceDBDataContext _dbContext;

        #region IProductRatingRepository Members

        public int CreateNewProductRating(Data.Entity.ProductRating theNewProductRating)
        {
            if (theNewProductRating == null)
            {
                return 0;
            }

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductRating_Create(theNewProductRating.ProductID, theNewProductRating.UserID, theNewProductRating.Rating, theNewProductRating.RatedOn);
			}

			return result;
		}

		public IList<Data.Entity.ProductRating> GetProductRatingByProduct(int productID)
		{
			if(productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetProductRatings_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProductRatings_By_Product(productID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductRating>();

				foreach(var pr in lstData)
				{
					var theEntity = new Data.Entity.ProductRating()
					{
						ProductID = pr.ProductID,
						ProductRatingID = pr.ProductRatingID,
						RatedOn = pr.RatedOn,
						Rating = pr.Rating,
						UserID = pr.UserID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

        #endregion
    }
}
